package cn.icare.cms.controller;


import cn.icare.cms.GuestBookService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.po.GuestBook;
import cn.icare.cms.feignclients.ADClient;
import cn.icare.cms.feignclients.UploadClient;
import cn.icare.cms.model.ro.AddGuestBookRO;
import cn.icare.cms.model.ro.AddShopCartRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/personal/guestbook")
@Api(tags = "REST意见反馈")
public class GuestBookController extends BaseController {

    @Autowired
    private UploadClient client;


    @Autowired
    private GuestBookService guestBookService;

    private final String FALL_STRING = "请求失败,服务繁忙!";


    private final Logger logger = LoggerFactory.getLogger(GuestBookController.class);


    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall") //增熔断器
    @ApiOperation(value = "提交意见反馈", notes = "提交意见反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "图片", required = false, paramType = "query", dataTypeClass = MultipartFile.class),
            @ApiImplicitParam(name = "message", value = "意见文字内容", required = true, paramType = "query", dataTypeClass = String.class),


    })
    public ResponseEntity<RespMsgVO> createOne(MultipartFile file, String message, HttpServletRequest request) {
        Long memberId = this.getCurrentUserId(request);
        if (memberId <= 0) {
            return super.getRespFail("会员身份验证信息不正确");
        }
        String kindName = "guestbook";
        String picurl = "";

        if (file != null && StringUtil.isNotEmpty(file.getOriginalFilename())) {
            ResponseEntity<RespMsgVO> uploadResult = this.client.uploadImage(file, kindName);
            if (uploadResult.getStatusCode() == HttpStatus.OK) {
                RespMsgVO msgVO = uploadResult.getBody();
                if (msgVO != null && StringUtil.isNotEmpty(msgVO.getResData().toString())) {
                    picurl = msgVO.getResData().toString();

                }
            } else {
                this.logger.error("图片上传失败:" + uploadResult.getStatusCodeValue());
                return super.getRespFail("图片上传失败");
            }
        }
        GuestBook book = new GuestBook();
        book.setStatus(1);
        book.setMemberid(memberId);
        book.setMessage(message);
        book.setPics(picurl);
        Integer result = this.guestBookService.createOne(book);
        if (result > 0) {
            return super.getRespOk();
        }
        return super.getRespFail();
    }

    //#region

    public ResponseEntity<RespMsgVO> createOneFall(MultipartFile file, String message, HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    //#endregion

}
